/**
 * @Project : algorithm-learning
 * @Author : Ruoyu Wang
 * @User : Momenta
 * @DateTime : 2024/4/2 10:40
 */

#include <iostream>

struct ListNode {
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}

    explicit ListNode(int x) : val(x), next(nullptr) {}

    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class List {
private:
    ListNode *head;
public:
    List() : head(new ListNode(-1, nullptr)) {}

    void addFromHead(int x);

    void createList(int nums[], int n);

    void printList();
};

void List::addFromHead(int x) {
    auto *s = new ListNode(x);
    s->next = head->next;
    head->next = s;
}

void List::createList(int nums[], int n) {
    for (int i = n - 1; i >= 0; --i) {
        addFromHead(nums[i]);
    }
}

void List::printList() {
    ListNode *p = head->next;
    while (p != nullptr) {
        std::cout << p->val << " ";
        p = p->next;
    }
    std::cout << std::endl;
}